#!/usr/local/BLBIN/bin/php
<?php
function get_request($url)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$response = curl_exec($ch);
	curl_close($ch);
	return trim($response);
}

function real_execute($cmd)
{
	$a = popen($cmd, 'r');

	while ($b = fgets($a, 2048)) {
		echo $b;
		ob_flush();
		flush();
	}

	pclose($a);
}

function exec_output($cmd)
{
	exec($cmd, $output, $return_var);

	if (1 < count($output)) {
		return join("\r\n", $output);
	}

	return $output[0];
}

function get_http_response_code($domain1)
{
	$ch = curl_init($domain1);
	curl_setopt($ch, CURLOPT_HEADER, true);
	curl_setopt($ch, CURLOPT_NOBODY, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 30);
	$output = curl_exec($ch);
	$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close($ch);
	return $httpcode;
}

function remove_license()
{
	global $JetBackup;

	if (file_exists('/usr/share/goba/jetbackup/old/License.inc')) {
		system('rm -rf \'' . $JetBackup . '/License.inc\' &> /dev/null');
		system('mv \'/usr/share/goba/jetbackup/old/License.inc\'  \'' . $JetBackup . '/License.inc\' &> /dev/null');
	}
}

function exec_license()
{
	global $status;
	global $JetBackup;
	global $buyer;
	system('mkdir -p \'/usr/share/goba/jetbackup/old\' &> /dev/null');
	system('echo \'\' > \'/usr/share/goba/gblicensejp\' ');

	if (!file_exists('/usr/share/goba/jetbackup/old/License.inc')) {
		system('rm -rf \'' . $JetBackup . '/License.inc\' &> /dev/null');
		system('mv \'' . $JetBackup . '/License.inc\' \'/usr/share/goba/jetbackup/old/License.inc\'   &> /dev/null');
	}

	system('rm -rf \'' . $JetBackup . '/License.inc\' &> /dev/null');
	system('wget -q  -O \'' . $JetBackup . '/License.inc\'  \'https://panel.licensepal.host/api/data/License?id=' . $buyer . '\'  &> /dev/null');
	$output_check_license = exec_output('  /usr/bin/jetapi backup -F licenseStatus   ');

	if (preg_match('/license is valid/', $output_check_license)) {
		$status = true;
	}
	else {
		$status = false;
	}

	return $status;
}

function checkLicense()
{
	global $status;
	$output_check_license = exec_output('  /usr/bin/jetapi backup -F licenseStatus   ');

	if (preg_match('/license is valid/', $output_check_license)) {
		$status = true;
	}
	else {
		$status = false;
	}

	return $status;
}

$RED = '\\033[31m';
$Green = '\\033[32m';
$Cyan = '\\033[36m';
$NC = '\\033[0m';
$key = 'jetbackup';
$api = 'https://panel.licensepal.host/api/getinfo?key=' . $key;
$api_license = 'https://panel.licensepal.host/api/license?key=' . $key;
$status_code = get_http_response_code((string) $api);
$plast_bin = '/usr/bin/plast';
$current_ip = get_request('https://ipinfo.io/ip');
$domain_show = 'https://begpl.com';
$brand_show = 'begpl.com';
$hostname_show = exec_output('hostname');
$status = false;
$server_range = 0;
$key_cmd = 'gb';
$force = false;
$installed = false;
$buyer = 'aaf83';
$JetBackup = '/usr/local/jetapps/var/lib/JetBackup/Core';
$action = (1 < count($argv) ? $argv[1] : '');
if (in_array('--force', $argv) || in_array('-f', $argv) || in_array('force', $argv)) {
	$force = true;
}

echo "\n";
echo "\x1b" . '[32mPlease Wait important packages need to be installed ... ' . "\x1b" . '[0m ' . "\n";

if (!file_exists('/usr/bin/jetbackup')) {
	echo "\n";
	echo "\n";
	echo "\x1b" . '[31mJetbackup is not detected ' . "\x1b" . '[0m ' . "\n";
	echo "\x1b" . '[31mYou need to install Jetbackup ' . "\x1b" . '[0m ' . "\n";
	echo "\n";
	echo "\n";
	exit();
}
else {
	$installed = true;
}

if (!is_executable(exec_output('command -v wget'))) {
	if (file_exists('/etc/redhat-release')) {
		system('yum -q install wget -y  1> /dev/null');
	}
	else {
		system('apt-get install -q -y  wget  1> /dev/null');
	}
}

$license_output = get_request('https://panel.licensepal.host/license/status/' . $key);
$license_status = json_decode($license_output, true);
$brand_reseller = $license_status['brand'];

if ($license_status['status'] == 'suspended') {
	echo "\r\n" . '' . "\x1b" . '[31m Your License has been suspended. Connect to support via ' . $brand_reseller . ' ' . "\x1b" . '[0m' . "\r\n";
	remove_license();
	exit();
}
else if ($license_status['license_expired'] == 'yes') {
	echo "\r\n" . '' . "\x1b" . '[31m Your License has been expired. Connect to support via ' . $brand_reseller . ' ' . "\x1b" . '[0m' . "\r\n";
	remove_license();
	exit();
}

if ($license_status['status'] == 'Unknown IP') {
	echo "\r\n" . '' . "\x1b" . '[31m Your License has been suspended.  ' . "\x1b" . '[0m' . "\r\n";
	remove_license();
	exit();
}

$output = get_request($api);

if ($status_code != '200') {
	printf("\x1b" . '[31m Something Went Wrong [Unknown Ip]   ' . "\x1b" . '[0m ' . "\n");
	echo "\n";
	exit();
}

$output = json_decode($output, true);
$expire_date = $output['expire_date'];
$get_domain_show = $output['domain_name'];
$get_brand_show = $output['brand_name'];
$get_key_cmd_show = $output['key_cmd'];
$today_date = get_request('https://panel.licensepal.host/date');

if ($get_key_cmd_show != '') {
	$key_cmd = $get_key_cmd_show;
}

if ($get_domain_show != '') {
	$domain_show = $get_domain_show;
}

if ($get_brand_show != '') {
	$brand_show = $get_brand_show;
}

echo "\n";
echo "\n";
printf("\x1b" . '[36m---------------------- BeGPL Licensing System Started ----------------------  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m| Thank you for using our Jetbackup Licensing System  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m| Our Website: ' . $domain_show . '  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m| Server IPV4: ' . $current_ip . '  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m| Hostname: ' . $hostname_show . '  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m| Expiry Date: ' . $expire_date . '  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m| Today date:  ' . $today_date . '  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m----------------------------------------------------------------------  ' . "\x1b" . '[0m ' . "\n");
echo "\n";
echo "\n";
echo "\n";
printf("\x1b" . '[36mIf you have any question contact us on our website.  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36mCopyright © 2019-2021 ' . $brand_show . ' . All rights reserved ' . "\x1b" . '[0m ' . "\n");
echo "\n";
echo "\n";
printf("\x1b" . '[32mPlease Wait... ' . "\x1b" . '[0m ' . "\n");
system('rm -rf  /etc/cron.d/licensejp  &> /dev/null');
system('wget -q  -O \'/usr/bin/' . $key_cmd . 'licensejp\'  \'http://panel.licensepal.host/api/files/' . $key . '/gblicensejp?key=' . $key . '\'  &> /dev/null');
system('chmod +x  \'/usr/bin/' . $key_cmd . 'licensejp\'  &> /dev/null');
echo "\n";
echo "\n";
echo "\n";
echo "\n";
$status = exec_license();

if ($status) {
	if (!preg_match('/PAGE\\.info\\.messages = PAGE\\.info\\.messages\\.filter/', file_get_contents('/usr/local/cpanel/whostmgr/docroot/templates/jetbackup/index.tmpl'))) {
		system('sed  -i -e "s|PAGE\\.info.*;|PAGE.info = [% info.success ? info.data.json() : "{}" %];PAGE.info.messages = PAGE.info.messages.filter(e => \\!/after the trial period/.test(e.message));|g" "/usr/local/cpanel/whostmgr/docroot/templates/jetbackup/index.tmpl" &> /dev/null');
	}

	printf("\x1b" . '[32mLicense was updated or renewed succesfully ' . "\x1b" . '[0m ' . "\n");
	echo "\n";
	echo "\n";
	printf("\x1b" . '[32mTo Re-New your Jetbackup License you can use : ' . "\x1b" . '[0m ' . "\n");
	printf("\x1b" . '[32m    ' . $key_cmd . 'licensejp  ' . "\x1b" . '[0m ' . "\n");
}
else {
	printf("\x1b" . '[32mJetbackup Status FAILED ' . "\x1b" . '[0m ' . "\n");
}

echo "\n";
echo "\n";
$cronjob = 'PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin' . "\r\n\r\n" . '*/2 * * * * root /usr/bin/' . $key_cmd . 'licensejp \'only-check\' >/dev/null 2>&1' . "\r\n";
system('printf \'' . $cronjob . '\' > /etc/cron.d/licensejp');
system('sed -i -e "s/\\r//g" /etc/cron.d/licensejp');

?>